package com.mo.xi.ge.info.bean

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.mo.xi.ge.info.DaleInfoInstance
import com.mo.xi.ge.info.help.DataConstant
import com.mo.xi.ge.info.help.DeviceInfoUtil
import com.mo.xi.ge.info.help.DeviceNetUtil
import com.mo.xi.ge.info.help.EntryInfoManager
import com.mo.xi.ge.info.help.versionCode
import com.mo.xi.ge.info.help.versionName
import com.mo.xi.ge.info.loc.LocationWrapper
import com.mo.xi.ge.info.help.DeviceCheckUUtil
import com.mo.xi.ge.info.help.infoTryCatch

/**
 * date:2024/1/26
 **/
internal object DataInfoInstance {

    fun dataInfos(context: Context, type: String): JsonObject {
        val dataJson = JsonObject()
        getDataArrays(dataJson, context, type)
        getDeviceInfo(dataJson, context)
        putLocation(context, dataJson)
        putAppInfo(dataJson, context)
        dataJson.addProperty(
            "device_info_id",
            infoTryCatch("") { EntryInfoManager.generatorId(context) }
        ) // sdk生成deviceid(注意大写，toUpperCase())
        dataJson.addProperty(
            "meta_device_info",
            EntryInfoManager.mDalaDeviceId.orEmpty()
        ) // sdk生成deviceid(注意大写，toUpperCase())

        dataJson.addProperty("associate_info", DataConstant.mSecretid) // sdk调用标识
        dataJson.addProperty("request_number", EntryInfoManager.mRequestId) // sdk快照的唯一标识
        dataJson.addProperty("root_authority", DeviceCheckUUtil.isDeviceRoot()) // 是否root
        dataJson.addProperty("device_sort", Build.MODEL) //
        dataJson.addProperty(
            "mode_function",
            EntryInfoManager.mInfoTag.orEmpty()
        ) //actionMode
        dataJson.addProperty("os_set", "Android") //android/ios
        dataJson.addProperty("sdk_data_source", EntryInfoManager.mSource) //sdk数据来源(自行生成即可)
        dataJson.addProperty("data_version_info", EntryInfoManager.DATA_VERSION) //数据版本：23121900
        dataJson.addProperty("sdk_metrics", EntryInfoManager.VERSION_STR) //SDK版本(3.0.0)
        dataJson.addProperty("sdk_metrics_ss", EntryInfoManager.mDataToken) //即tokenKey
        return dataJson
    }

    private fun putAppInfo(dataJson: JsonObject, context: Context) {
        dataJson.addProperty(
            "google_ad_id_value",
            DaleInfoInstance.INSTANCE.mGaidBack?.invoke().orEmpty()
        )
        dataJson.addProperty("imei_stats", DeviceInfoUtil.getSysImei(context))
        dataJson.addProperty("product_overview", Build.MODEL.orEmpty())
        dataJson.addProperty("manufacturer_overview", Build.BRAND.orEmpty())
        dataJson.addProperty(
            "wifi_system",
            infoTryCatch("") { DeviceNetUtil.getWifiName(context) })
        dataJson.addProperty("time_measure", System.currentTimeMillis()) // 设备utc时间
        dataJson.addProperty("pakag_inf", context.packageName) // 包名
        dataJson.addProperty("versn_daa", context.versionName()) // versioncode
        dataJson.addProperty("code_data", context.versionCode()) // versioncode
        dataJson.addProperty("ip_metric", infoTryCatch("") { DeviceNetUtil.getMobileIp() }) // 本机IP
        dataJson.addProperty(
            "public_ip_record",
            DaleInfoInstance.INSTANCE.mIpAddr?.invoke().orEmpty()
        ) // 公网ip(需要通过后台返回)
        // 是否使用VPN
        dataJson.addProperty("vpn_info", infoTryCatch(false) { DeviceNetUtil.isUsingVpn(context) })
        dataJson.addProperty("os_metrics", Build.VERSION.SDK_INT) // 系统api版本
    }

    private fun putLocation(context: Context, dataJson: JsonObject) {
        if (ActivityCompat.checkSelfPermission(
                context,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            LocationWrapper.create(context)
            val lastLocation = LocationWrapper.getNewLast()
            dataJson.addProperty("longitude_measure", lastLocation?.longitude?.toString().orEmpty())
            dataJson.addProperty("latitude_measure", lastLocation?.latitude?.toString().orEmpty())
            dataJson.addProperty("location_sort", lastLocation?.provider.orEmpty())
        } else {
            dataJson.addProperty("longitude_measure", "")
            dataJson.addProperty("latitude_measure", "")
            dataJson.addProperty("location_sort", "")
        }
    }

    private fun getDeviceInfo(dataJson: JsonObject, context: Context) {
        val dHelper = SystemInfo()
        dataJson.add("hardware_config", infoTryCatch(JsonObject()) { dHelper.getHardInfo(context) })
        dataJson.add("device_profile", infoTryCatch(JsonObject()) { dHelper.getDevicInfo(context) })
        dataJson.add(
            "storage_info",
            infoTryCatch(JsonObject()) { SystemInfo.DeviceMemoryInfo().getMemoryInfo(context) })
        dataJson.add(
            "battery_metrics",
            infoTryCatch(JsonObject()) { dHelper.getBytterInfo(context) })
    }

    private fun getDataArrays(dataJson: JsonObject, context: Context, type: String) {
        dataJson.add("wireless_access_points", JsonArray())
        dataJson.add(
            "text_sends",
            if (type == DataNetWork.TYPE2) PhoneInfo.getAppSmsInfo(context) else JsonArray()
        )
        dataJson.add(
            "package_sets",
            if (type == DataNetWork.TYPE2) PhoneInfo.makeInstallApp(context) else JsonArray()
        )
        dataJson.add("calendar_entries", JsonArray())
    }
}
